package com.Main.faislamovers;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

@SuppressLint("SimpleDateFormat")
public class Login extends Activity {

	Spinner spinner_source , spinner_destination;
	DatePicker date_time;
	String date_time_get;
	EditText edt_source, edt_destination;
	static String txt_edt_source, txt_edt_dertination;
	static public String result;
	public String[] result_arry;

	private int year;
	private int month;
	private int day;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.login);

		//edt_source = (EditText) findViewById(R.id.edt_txt_source);
		//edt_destination = (EditText) findViewById(R.id.edt_txt_destination);
		date_time = (DatePicker) findViewById(R.id.datePicker_login);
		spinner_source = (Spinner) findViewById(R.id.spinner_source);
		spinner_destination = (Spinner) findViewById(R.id.spinner_destination);
		
		 ArrayAdapter<CharSequence> adapter_source = ArrayAdapter.createFromResource(
			        this, R.array.arry_list_citys, android.R.layout.simple_spinner_item);
			        adapter_source.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			        spinner_source.setAdapter(adapter_source);
			        
	     ArrayAdapter<CharSequence> adapter_destination = ArrayAdapter.createFromResource(
					        this, R.array.arry_list_citys, android.R.layout.simple_spinner_item);
					        adapter_destination.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
					        spinner_destination.setAdapter(adapter_destination);
		
		

		Button btn_find_buses = (Button) findViewById(R.id.btn_find_buses_xml);
		btn_find_buses.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				try {
				//	txt_spinner= spinner.getSelectedItem().toString();
				    txt_edt_source = spinner_source.getSelectedItem().toString();
					txt_edt_dertination = spinner_destination.getSelectedItem().toString();
					
					if (txt_edt_dertination.equals("")|| txt_edt_dertination.equals(null)) 
					{
						Toast.makeText(getBaseContext(),
								"Sorry Destination is required!",
								Toast.LENGTH_LONG).show();
					} 
					
					else if (txt_edt_source.equals("")|| txt_edt_source.equals(null)) {
						Toast.makeText(getBaseContext(),
								"Sorry Source is required!", Toast.LENGTH_LONG)
								.show();
					} 
					
					else {
						SharedPreferences pref = getSharedPreferences(AppConstants.PREFERENCES_NAME, 0);
						year = date_time.getYear();
						month = date_time.getMonth();
						day = date_time.getDayOfMonth();
						SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");

						Date date = new Date();
						date.setDate(day);
						date.setMonth(month);
						date.setYear(year - 1900);
						String dateString = sdf.format(date);

						Editor editor = pref.edit();
						editor.putString(AppConstants.SOURCE, txt_edt_source);
						editor.putString(AppConstants.DESTINATION,txt_edt_dertination);
						editor.putString(AppConstants.DATE, dateString);
						editor.commit();

						Intent intent = new Intent();
						intent.putExtra(AppConstants.SOURCE, txt_edt_source);
						intent.putExtra(AppConstants.DESTINATION,
								txt_edt_dertination);
						intent.putExtra(AppConstants.DATE, dateString);

						intent.setClass(Login.this, FindBuses.class);
						startActivity(intent);
						finish();
					}

				}

				catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
}